package com.zmn.plat.dubbo.impl.netcall;

import javax.annotation.Resource;

import org.apache.dubbo.config.annotation.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.zmn.common.dto.ResponseDTO;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.common.utils.response.ResponseUtil;
import com.zmn.plat.business.interfaces.netcall.UserImAccountBService;
import com.zmn.plat.dubbo.dto.netcall.ImAccountDRO;
import com.zmn.plat.dubbo.interfaces.netcall.ImAccountRemoteService;
import com.zmn.plat.dubbo.utils.DubboConsts;
import com.zmn.plat.model.entity.netcall.UserImAccount;

/**
 * @author sunlife
 * @since 2019/7/12 19:27
 */
@Service(loadbalance = "roundrobin", timeout = 5000, retries = -1, version = DubboConsts.INTERFACE_VERSION, validation = "true")
public class ImAccountRemoteServiceImpl implements ImAccountRemoteService {

    Logger logger = LoggerFactory.getLogger(ImAccountRemoteServiceImpl.class);

    @Resource
    UserImAccountBService userImAccountBService;

    /**
     * 查询网易云信账户信息
     *
     * @param staffId
     * @return
     */
    @Override
    public ResponseDTO<ImAccountDRO> getAccountByStaffId(Integer staffId) {
        try {
            UserImAccount userImAccount = userImAccountBService.getAccountByStaffId(staffId);
            ImAccountDRO imAccountDRO = BeanMapper.map(userImAccount, ImAccountDRO.class);
            return ResponseUtil.responseSuccessData(imAccountDRO);
        } catch (Exception e) {
            logger.info("根据员工ID获取网易云信账户信息失败：" + e.getMessage());
            return ResponseUtil.responseFail(e.getMessage());
        }
    }

    /**
     * 冻结账户
     *
     * @param staffId
     * @return
     */
    @Override
    public ResponseDTO<Boolean> blockImAccount(Integer staffId) {
        try {
            Boolean result = userImAccountBService.blockAccountByStaffId(staffId);
            return ResponseUtil.responseSuccessData(result);
        } catch (Exception e) {
            logger.info("冻结账户失败：" + e.getMessage());
            return ResponseUtil.responseFail("冻结账户失败：" + e.getMessage());
        }
    }

    /**
     * 解冻账户
     *
     * @param staffId
     * @return
     */
    @Override
    public ResponseDTO<Boolean> unblockImAccount(Integer staffId) {
        try {
            Boolean result = userImAccountBService.unblockAccountByStaffId(staffId);
            return ResponseUtil.responseSuccessData(result);
        } catch (Exception e) {
            logger.info("解冻账户失败：" + e.getMessage());
            return ResponseUtil.responseFail("解冻账户失败：" + e.getMessage());
        }
    }

    /**
     * 更新用户名
     *
     * @param staffId
     * @param name
     * @return
     */
    @Override
    public ResponseDTO<Boolean> updateNameByStaffId(Integer staffId, String name) {
        try {
            Boolean result = userImAccountBService.updateAcctName(staffId, name);
            return ResponseUtil.responseSuccessData(result);
        } catch (Exception e) {
            logger.info("更新用户名失败：" + e.getMessage());
            return ResponseUtil.responseFail("更新用户名失败：" + e.getMessage());
        }
    }
}
